﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace PinnedSiteGenerator.PinnedSiteGenerator
{
    [ToolboxItem(false)]
    public partial class PinnedSiteGenerator : System.Web.UI.WebControls.WebParts.WebPart
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        
        #region Properties
        private string categoryName;
        private string item1Name;
        private string item1Url;

        private string item2Name;
        private string item2Url;

        private string item3Name;
        private string item3Url;

        private string item4Name;
        private string item4Url;

        private string item5Name;
        private string item5Url;

        [WebBrowsable(true)]
        [WebDisplayName("Category Name")]
        [Personalizable(true)]
        public string CategoryName
        {
            get { return categoryName; }
            set { categoryName = value; }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Item #1 Name")]
        [Personalizable(true)]
        public string Item1Name
        {
            get { return item1Name; }
            set { item1Name = value; }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Item #1 Url")]
        [Personalizable(true)]
        public string Item1Url
        {
            get { return item1Url; }
            set { item1Url = value; }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Item #2 Name")]
        [Personalizable(true)]
        public string Item2Name
        {
            get { return item2Name; }
            set { item2Name = value; }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Item #2 Url")]
        [Personalizable(true)]
        public string Item2Url
        {
            get { return item2Url; }
            set { item2Url = value; }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Item #3 Name")]
        [Personalizable(true)]
        public string Item3Name
        {
            get { return item3Name; }
            set { item3Name = value; }
        }


        [WebBrowsable(true)]
        [WebDisplayName("Item #3 Url")]
        [Personalizable(true)]
        public string Item3Url
        {
            get { return item3Url; }
            set { item3Url = value; }
        }


        [WebBrowsable(true)]
        [WebDisplayName("Item #4 Name")]
        [Personalizable(true)]
        public string Item4Name
        {
            get { return item4Name; }
            set { item4Name = value; }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Item #4 Url")]
        [Personalizable(true)]
        public string Item4Url
        {
            get { return item4Url; }
            set { item4Url = value; }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Item #5 Name")]
        [Personalizable(true)]
        public string Item5Name
        {
            get { return item5Name; }
            set { item5Name = value; }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Item #5 Url")]
        [Personalizable(true)]
        public string Item5Url
        {
            get { return item5Url; }
            set { item5Url = value; }
        }
        #endregion

        protected override void Render(HtmlTextWriter writer)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<script type='text/javascript'>");
            sb.AppendLine("window.external.msSiteModeCreateJumplist('" + this.categoryName + "');");
            if (Item1Name != null && item1Url != null)
                sb.AppendLine("window.external.msSiteModeAddjumplistItem('" + this.item1Name + "','" + this.item1Url + "', '/_layouts/images/favicon.ico', 'self');");

            if (Item2Name != null && item2Url != null)
                sb.AppendLine("window.external.msSiteModeAddjumplistItem('" + this.item2Name + "','" + this.item2Url + "', '/_layouts/images/favicon.ico', 'self');");

            if (Item3Name != null && item3Url != null)
                sb.AppendLine("window.external.msSiteModeAddjumplistItem('" + this.item3Name + "','" + this.item3Url + "', '/_layouts/images/favicon.ico', 'self');");

            if (Item4Name != null && item4Url != null)
                sb.AppendLine("window.external.msSiteModeAddjumplistItem('" + this.item4Name + "','" + this.item4Url + "', '/_layouts/images/favicon.ico', 'self');");

            if (Item5Name != null && item5Url != null)
                sb.AppendLine("window.external.msSiteModeAddjumplistItem('" + this.item5Name + "','" + this.item5Url + "', '/_layouts/images/favicon.ico', 'self');");

            sb.AppendLine("window.external.msSiteModeShowJumplist();");
            sb.Append("</script>");
            writer.Write(sb.ToString());


        }
    }
}
